package com.situ.shoplookweb.controller;

import com.situ.shoplook.common.model.ShoppingCart;
import com.situ.shoplook.common.model.User;
import com.situ.shoplook.common.service.GoodsInfoService;
import com.situ.shoplookweb.service.ShoppingCartService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

/**
 * @Author xinchen
 * @Date: 2024/09/05/ 16:27
 * @Description 跳转购物车
 */
@Controller
public class ShopCatController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private GoodsInfoService goodsInfoService;
    /**
     * 回显购物车
     * @param session
     */
    @GetMapping("/shop-cart")
    public String shopCartPage(HttpServletRequest request, HttpSession session) {
        User user = (User) session.getAttribute("newBeeMallUser");
        /**
         * 购物车商品总数和总价
         */
        int itemsTotal = 0;
        int priceTotal = 0;
        List<ShoppingCart> myshopCart = shoppingCartService.getMyShoppingCartItems(user.getUserId());
        //获取购物车商品
        if(!CollectionUtils.isEmpty(myshopCart)){
            //购物车总数
            itemsTotal = myshopCart.stream().mapToInt(ShoppingCart::getGoodsCount).sum();
            if(itemsTotal < 1){
                throw new RuntimeException("购物车为空");
            }

            //计算总价格
            priceTotal = myshopCart.stream().mapToInt(item -> item.getGoodsCount() * item.getGoodsInfo().getSellingPrice()).sum();
            if (priceTotal < 1){
                throw new RuntimeException("购物车总价为空");
            }
        }
        System.out.println("myshopCart = " + myshopCart.toString()+ "itemsTotal = " + itemsTotal + "priceTotal = " + priceTotal);
        // 封装数据
        request.setAttribute("itemsTotal",itemsTotal);
        request.setAttribute("priceTotal",priceTotal);
        request.setAttribute("myShoppingCartItems",myshopCart);
        return "mall/cart";
    }
    /**
     * 订单结算
     */
    @GetMapping("/shop-cart/settle")
    public String settle(HttpServletRequest request, HttpSession session) {
        User user = (User) session.getAttribute("newBeeMallUser");
        int priceTotal = 0; // 总价
        List<ShoppingCart> myshopCart = shoppingCartService.getMyShoppingCartItems(user.getUserId());
        if(CollectionUtils.isEmpty(myshopCart)){
            return "mall/cart";
        }else {
            //总价
            priceTotal = myshopCart.stream().mapToInt(item -> item.getGoodsCount() * item.getGoodsInfo().getSellingPrice()).sum();
            if(priceTotal < 1){
                throw new RuntimeException("购物车总价为空");
            }
        }
        request.setAttribute("priceTotal",priceTotal);
        request.setAttribute("myShoppingCartItems",myshopCart);
        return "mall/order-settle";
    }
}
